/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ejercicios7_3y4;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.EOFException;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Scanner;
import consola.Consola;
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.ArrayList;

/**
 *
 * @author Mañana
 */
public class Ejercicios7_3y4 {

    static String fichero = "./Enteros.txt";

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws FileNotFoundException {
        escribirTXT();
        escribirBIN();
        leer();
    }

    private static void escribirTXT() {
        try {
            File ficheroNombre = new File(fichero);
            FileOutputStream filein = new FileOutputStream(fichero);
            DataOutputStream dataOS = new DataOutputStream(filein);
            Scanner s = new Scanner(System.in);
            System.out.println("Introduzca un Numero:");
            while (s.hasNext()) {
                System.out.println("Introduzca un Numero:");
                dataOS.writeInt(s.nextInt());
            }
            dataOS.close();
        } catch (Exception io) {
            System.out.println("Error S/N");
        }
    }

    private static void escribirBIN() throws FileNotFoundException {
        try {
            //Declaramos el Archivo
            //Buscamos el Fichero que queremos mostrar
            File newTextFile = new File("./Enteros.txt");
            File newTextFile2 = new File("./Enteros.bin");
            FileOutputStream fos = new FileOutputStream(newTextFile2);
            DataOutputStream fosD =new DataOutputStream(fos);
            FileInputStream fis = new FileInputStream(newTextFile);
            DataInputStream fosI =new DataInputStream(fis);
            BufferedReader fr = new BufferedReader(new FileReader(newTextFile));
            ArrayList<Integer> numeros = new ArrayList<>();
            int i = 0;
            String num = " ";
            boolean leonum = false;
            while ((i = fr.read()) != -1) {
                if (i == ';' || i == ' ') {
                    if (leonum = true) {
                        leonum = false;
                        numeros.add(Integer.parseInt(num));
                        num = "";
                    }
                } else {
                    if (leonum == false) {
                        num += (char) i;
                        leonum = true;
                    } else {
                        num += (char) i;
                    }
                }
            }
            for(int j=0;j<numeros.size();j++){
            fosD.writeInt(numeros.get(j));    
            }
            while(true){
                fosI.readInt();
            }

        } catch (IOException ex) {
            System.out.println("Excepcion de IO.");
        }
    }

    private static void leer() {
        String ficheroLeer = Consola.leerString("Introduzca fichero a leer.");
        try {
            FileInputStream fileout = new FileInputStream(ficheroLeer);
            DataInputStream dataIS = new DataInputStream(fileout);
            int n;
            try {
                while (true) {
                    n = dataIS.readInt();
                    System.out.println("Numero: " + n);// Devuelve el Numero
                }
            } catch (EOFException eo) {
            } finally {
                dataIS.close();
            }
        } catch (FileNotFoundException fnfe) {
            System.out.println("El fichero no Existe");
        } catch (IOException ioe) {
            System.out.println("Error de E/S");
        }
    }
}
